@echo off
REM Flowable 流程部署脚本 (Windows)
REM 用于批量部署所有 BPMN 流程文件

setlocal enabledelayedexpansion

set FLOWABLE_URL=%FLOWABLE_URL%
if "%FLOWABLE_URL%"=="" set FLOWABLE_URL=http://localhost:8081/flowable-rest

set FLOWABLE_USER=%FLOWABLE_USER%
if "%FLOWABLE_USER%"=="" set FLOWABLE_USER=admin

set FLOWABLE_PASS=%FLOWABLE_PASS%
if "%FLOWABLE_PASS%"=="" set FLOWABLE_PASS=test

set PROCESSES_DIR=.\flowable\processes

echo ==========================================
echo Flowable 流程部署脚本
echo ==========================================
echo Flowable URL: %FLOWABLE_URL%
echo 用户名: %FLOWABLE_USER%
echo 流程目录: %PROCESSES_DIR%
echo ==========================================
echo.

REM 检查 Flowable 服务是否可用
echo 检查 Flowable 服务状态...
curl -s -u "%FLOWABLE_USER%:%FLOWABLE_PASS%" "%FLOWABLE_URL%/service/management/engine" >nul 2>&1
if errorlevel 1 (
    echo 错误: 无法连接到 Flowable 服务，请确保服务已启动
    exit /b 1
)
echo Flowable 服务连接成功
echo.

REM 部署所有 BPMN 文件
echo 开始部署流程...
set deployed=0
set failed=0

for %%f in ("%PROCESSES_DIR%\*.bpmn20.xml") do (
    set filename=%%~nxf
    echo 部署: !filename!
    
    curl -s -w "\n%%{http_code}" -X POST ^
        -u "%FLOWABLE_USER%:%FLOWABLE_PASS%" ^
        -F "file=@%%f" ^
        "%FLOWABLE_URL%/service/repository/deployments" > temp_response.txt 2>&1
    
    for /f "tokens=*" %%a in (temp_response.txt) do set response=%%a
    del temp_response.txt
    
    REM 提取 HTTP 状态码（简单处理，实际可能需要更复杂的解析）
    echo !response! | findstr /C:"201" >nul
    if !errorlevel! equ 0 (
        echo   ✓ 部署成功
        set /a deployed+=1
    ) else (
        echo   ✗ 部署失败
        set /a failed+=1
    )
    echo.
)

echo ==========================================
echo 部署完成
echo 成功: %deployed%
echo 失败: %failed%
echo ==========================================

if %failed% gtr 0 (
    exit /b 1
)

